public class Demo15 {
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();

        Thread t1 = new Thread(()->{
            synchronized (locker1){
                // 这里的sleep非常重要,要确保t1 和 t2 都分别拿到一把锁之后,再进行后续动作
                // 否则就会出现t1很快对两把锁都加锁了的情况,就不会出现死锁的情况了
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (locker2){
                    System.out.println("t1加锁成功!");
                }
            }
        });

        Thread t2 = new Thread(()->{
           synchronized (locker1){
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }

               synchronized (locker2){
                   System.out.println("t2加锁成功!");
               }
           }
        });

        t1.start();
        t2.start();
    }
}
